<%@ page import="slh.Book.Book" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑图书</title>
</head>
<body>
<h1>编辑图书</h1>
<%
    Integer bookId = null;
    Book book = null;

    try {
        bookId = Integer.valueOf(request.getParameter("id"));
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/webproject_2025", "root", "root");
             PreparedStatement stmt = conn.prepareStatement("SELECT * FROM book WHERE BOOK_ID=?")) {
            stmt.setInt(1, bookId);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    book = new Book();
                    book.setBookId(rs.getInt("BOOK_ID"));
                    book.setTitle(rs.getString("TITLE"));
                    book.setAuthor(rs.getString("AUTHOR"));
                    book.setPublisher(rs.getString("PUBLISHER"));
                    book.setPublishYear(rs.getInt("PUBLISH_YEAR"));
                    book.setStatus(rs.getString("STATUS"));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
%>
<p>获取图书信息失败: <%= e.getMessage() %></p>
<%
        return;
    }

    if (book == null) {
%>
<p>图书不存在</p>
<%
        return;
    }
%>

<form action="/admin/updateBook" method="post">
    <input type="hidden" name="bookId" value="<%= book.getBookId() %>">

    <label>标题: </label>
    <input type="text" name="title" value="<%= book.getTitle() %>"><br>

    <label>作者: </label>
    <input type="text" name="author" value="<%= book.getAuthor() %>"><br>

    <label>出版社: </label>
    <input type="text" name="publisher" value="<%= book.getPublisher() %>"><br>

    <label>出版年份: </label>
    <input type="number" name="publishYear" value="<%= book.getPublishYear() %>"><br>

    <label>状态: </label>
    <select name="status">
        <option value="normal" <%= "normal".equals(book.getStatus()) ? "selected" : "" %>>正常</option>
        <option value="deleted" <%= "deleted".equals(book.getStatus()) ? "selected" : "" %>>已删除</option>
    </select><br>

    <input type="submit" value="保存">
</form>
</body>
</html>